package com.mew.slots.core.common.messge.response;

import com.alibaba.fastjson.JSON;
import com.mew.common.entity.game.Jackport;
import com.mew.common.rest.bo.GameSession;
import com.mew.common.rest.game.SlotsEnterRoomResponse;
import com.mew.slots.core.common.handler.RoomBO;
import com.mew.slots.core.common.handler.RoomInfoManager;
import com.zebra.common.ZebraBaseMessage;
import io.netty.buffer.ByteBuf;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.util.List;


@Data
@ToString(callSuper = true)
@NoArgsConstructor
public class EnterGameResponseMessage extends ZebraBaseMessage<EnterGameResponseMessage> {

    private UserMessage userMessage;
    private int[] roomBetArray;
    private long lastBetNumber; // 上把 押注
    private int lastLineNumber; //上把 线数
    private int[] lastIcon;   //上把 图标
    private WinFreeMessage winFreeMessage;
    private List<Jackport> jackPorts;//奖池值


    @Override
    public void write(ByteBuf b) {

        userMessage.write(b);
        writeInt(b, this.roomBetArray.length);
        for (int i=0; i<roomBetArray.length; i++){
            writeInt(b, this.roomBetArray[i]);
        }
        writeLong(b, this.lastBetNumber);
        writeInt(b, this.lastLineNumber);

        writeInt(b, this.lastIcon.length);
        for (int i=0; i<lastIcon.length; i++){
            writeInt(b, this.lastIcon[i]);
        }

        winFreeMessage.write(b);
        if(jackPorts==null){
            writeInt(b, 0);
        }else {
            writeInt(b, jackPorts.size());
            for (Jackport jackPort : jackPorts) {
                writeInt(b, jackPort.getNo());
                writeLong(b, jackPort.getVal());
            }
        }
    }

    @Override
    public EnterGameResponseMessage read(ByteBuf b) {
        this.userMessage = new UserMessage().read(b);
        return this;
    }


    public EnterGameResponseMessage buildData(GameSession slotsSession, SlotsEnterRoomResponse roomResponse){
        RoomBO roomBO = RoomInfoManager.getInstance().get(slotsSession.getRoomId());
        this.userMessage = UserMessage.builder()
                .uid(slotsSession.getUserId())
                .gid(slotsSession.getGid())
                .headPath(slotsSession.getHeadPath())
                .nickname(slotsSession.getNickname())
                .goldNumber(slotsSession.getGoldNumber())
                .build();

        this.setRoomBetArray(roomBO.getBetArray());
        this.setLastBetNumber(roomResponse.getLastBetNumber());
        this.setLastLineNumber(roomResponse.getLastLineNumber());


        try {
            this.setLastIcon(JSON.parseArray(roomResponse.getLastIcon(),Integer.class).stream().mapToInt(Integer::intValue).toArray());
        } catch (Exception e) {
            this.setLastIcon(new int[0]);
        }

        this.winFreeMessage = WinFreeMessage.builder()
                .frees(roomResponse.getFrees())
                .freeCount(roomResponse.getFreeCount())
                .freeNumber(roomResponse.getFreeNumber())
                .freeRate(roomResponse.getFreeRate())
                .freeType(roomResponse.getFreeType())

                .bonusBibei(roomResponse.getBonusBibei())
                .bonusChange(roomResponse.getBonusChange())
                .bonusNumber(roomResponse.getBonusNumber())
                .bonusRate(roomResponse.getBonusRate())
                .bonusType(roomResponse.getBonusType())

                .state(roomResponse.getState())
                .json(roomResponse.getJson())
                .lastWinNumber(roomResponse.getLastWinNumber())
                .build();
        this.jackPorts=roomResponse.getJackports();
        return this;

    }

    public String toSimpleString(int roomId){
        StringBuilder buf = new StringBuilder(64);
        buf.append(" uid: ").append(this.getUserMessage().getUid())
                .append(" goldNumber: ").append(this.getUserMessage().getGoldNumber())
                .append(" roomId: ").append(roomId)
                .append(" freeNumber: ").append(this.getWinFreeMessage().getFreeNumber())
                .append(" bonusNumber: ").append(this.getWinFreeMessage().getBonusNumber());
        return buf.toString();
    }
}





